package com.googlecode.openauth.web.action;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.googlecode.openauth.web.dao.IActionDao;
import com.googlecode.openauth.web.dao.IAreaDao;
import com.googlecode.openauth.web.dao.IResourceDao;
import com.googlecode.openauth.web.dao.IRoleActionDao;
import com.googlecode.openauth.web.dao.IRoleAreaDao;
import com.googlecode.openauth.web.dao.IRoleDao;
import com.googlecode.openauth.web.dao.IRoleResDao;
import com.googlecode.openauth.web.dao.IUserDao;
import com.googlecode.openauth.web.dao.IUserRoleDao;
import com.googlecode.openauth.web.entity.ActionInfo;
import com.googlecode.openauth.web.entity.AreaInfo;
import com.googlecode.openauth.web.entity.ResourceInfo;
import com.googlecode.openauth.web.entity.RoleAction;
import com.googlecode.openauth.web.entity.RoleArea;
import com.googlecode.openauth.web.entity.RoleInfo;
import com.googlecode.openauth.web.entity.RoleResource;
import com.googlecode.openauth.web.entity.UserInfo;
import com.googlecode.openauth.web.entity.UserRole;
import com.googlecode.openauth.web.util.Util;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7272305084904191922L;

	private IUserDao userDao;
	private IUserRoleDao userRoleDao;
	private IRoleDao roleDao;
	private IRoleActionDao roleActionDao;
	private IActionDao actionDao;
	private IRoleResDao roleResDao;
	private IResourceDao resourceDao;
	private IRoleAreaDao roleAreaDao;
	private IAreaDao areaDao;
	private String username;
	private String password;

	private static Logger logger = Logger.getLogger(LoginAction.class);

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	private String url;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public IUserRoleDao getUserRoleDao() {
		return userRoleDao;
	}

	public void setUserRoleDao(IUserRoleDao userRoleDao) {
		this.userRoleDao = userRoleDao;
	}

	public IRoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public IRoleActionDao getRoleActionDao() {
		return roleActionDao;
	}

	public void setRoleActionDao(IRoleActionDao roleActionDao) {
		this.roleActionDao = roleActionDao;
	}

	public IActionDao getActionDao() {
		return actionDao;
	}

	public void setActionDao(IActionDao actionDao) {
		this.actionDao = actionDao;
	}

	public IRoleResDao getRoleResDao() {
		return roleResDao;
	}

	public void setRoleResDao(IRoleResDao roleResDao) {
		this.roleResDao = roleResDao;
	}

	public IRoleAreaDao getRoleAreaDao() {
		return roleAreaDao;
	}

	public void setRoleAreaDao(IRoleAreaDao roleAreaDao) {
		this.roleAreaDao = roleAreaDao;
	}

	public IAreaDao getAreaDao() {
		return areaDao;
	}

	public void setAreaDao(IAreaDao areaDao) {
		this.areaDao = areaDao;
	}

	/**
	 * 鎵ц鐢ㄦ埛閴存潈鍒ゆ柇銆�濡傛灉璇锋眰灞炴�涓甫鏈塻ourcePage鍙傛暟锛屽垯鐧诲綍鎴愬姛鍚庤烦杞埌sourcePage鎸囨槑鐨勯〉闈�
	 * 閴存潈鎴愬姛鍚庯紝灏嗘鐢ㄦ埛鎵�嫢鏈夌殑鏉冮檺淇℃伅浠ユ湇鍔″櫒闂碒TTP璇锋眰post缁欐寚瀹氱殑URL鍦板潃銆傛URL鍦板潃鍙互鏄涓�
	 * 杩欎簺URL鍦板潃鍙互娉ㄥ唽鍦ㄥ睘鎬ф枃浠朵腑锛屼篃鍙槸娉ㄥ唽鍦╳eb.xml鏂囦欢涓�
	 * 鑾峰彇HttpRequest绛夊璞＄殑鏂规硶锛屽彲鍙傝�http://pengfeng.iteye.com/blog/212358
	 */
	public String execute() {
		//return login();
		return ERROR;
	}

	public String login() {
		String userName = getUsername();
		String password = getPassword();
		if (password == null) {
			logger.info(" password is null !! ");
			return ERROR;
		}
		// ServletActionContext.getRequest ();

		String encodedPassword = Util.encode(password);

		UserInfo user = userDao.getUserByName(userName);
		if (user == null) {
			return ERROR;
		}

		String dbPassword = user.getPassword();
		if (StringUtils.isEmpty(dbPassword)) {
			return ERROR;
		}

		if (dbPassword.equals(encodedPassword)) {
			ActionContext context = ActionContext.getContext();
			Map<String, Object> session = context.getSession();
			session.put("user", user);

			long userId = user.getUserId();
			List<UserRole> userRoles = userRoleDao
					.getUserRoleByUserId((int) userId);

			// user.setUserRoles(userRoles);

			for (UserRole userRole : userRoles) {
				int roleId = userRole.getRoleId();
				RoleInfo roleInfo = roleDao.getRoleById(roleId);
				if (roleInfo == null) {
					continue;
				}

				List<RoleInfo> userRoleInfos = user.getRoleInfos();
				userRoleInfos.add(roleInfo);
				List<RoleAction> roleActions = roleActionDao
						.getRoleActionByRoleId(roleInfo.getRoleId());

				for (RoleAction roleAction : roleActions) {
					int actionId = roleAction.getActionId();
					ActionInfo actionInfo = actionDao.getActionById(actionId);
					roleInfo.getRoleAction().add(actionInfo);
				}

				List<RoleResource> roleRess = roleResDao
						.getRoleResByRoleId(roleInfo.getRoleId());
				for (RoleResource roleRes : roleRess) {
					int resId = roleRes.getResourceId();
					ResourceInfo resInfo = resourceDao.getResourceById(resId);
					roleInfo.getRoleRes().add(resInfo);
				}

				List<RoleArea> roleAreas = roleAreaDao
						.getRoleAreaByRoleId(roleInfo.getRoleId());
				for (RoleArea roleArea : roleAreas) {
					int areaId = roleArea.getAreaId();
					AreaInfo areaInfo = areaDao.getAreaLevelByid(areaId);
					roleInfo.getRoleArea().add(areaInfo);
				}
			}
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
	
	public String loginOut(){
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		session.put("user", null);
		return SUCCESS;
	}

	public IResourceDao getResourceDao() {
		return resourceDao;
	}

	public void setResourceDao(IResourceDao resourceDao) {
		this.resourceDao = resourceDao;
	}
}
